<template>
  <div style="padding:20px;">
    <span style="margin-bottom: 10px;">授权</span>
    <el-divider></el-divider>
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm dict-form">
      <el-card
        v-for="(value,key) in permissions"
        :key="key"
        :body-style="{ padding: '0px', border: 'none' }"
        shadow="never"
        class="box-card"
        style="border-radius: 0;margin-bottom: -1px;"
      >
        <div class="el-card__header">
          <span>{{ key }}</span>
        </div>
        <div style="padding: 0 10px 10px 10px;">
          <el-checkbox-group v-model="formData.permissionCodes" size="small" porp="permissionRules">
            <el-checkbox v-for="item in value" :key="item.permissionCode" :label="item.permissionCode" style="margin-top: 10px;margin-left: 0;margin-right: 15px;" name="permissionCodes">{{ item.permissionName }}</el-checkbox>
          </el-checkbox-group>
        </div>
      </el-card>
      <el-form-item style="text-align: center;margin-top: 20px;">
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="$router.back(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getPermission, getPermissionByRoleCode, setPermission } from '@/api/sys/auth/role/index'
export default {
  data() {
    return {
      checkAll: false,
      permissions: [],
      formData: {
        permissionCodes: [],
        roleCode: this.$route.query.roleCode
      },
      rules: {
        permissionRules: {

        }
      }
    }
  },
  created() {
    if (!this.$route.query.roleCode) {
      this.$router.push({ name: 'role_mgr_list' })
    } else {
      this.getData()
    }
  },
  methods: {
    getData() {
      getPermission().then(res => {
        this.permissions = res.data
        getPermissionByRoleCode(this.$route.query.roleCode).then(res => {
          this.formData.permissionCodes = res.data
        })
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          setPermission(this.formData).then(() => {
            this.loading = false
            // 提示信息
            this.$message({
              showClose: true,
              message: '配置权限成功',
              type: 'success'
            })
            this.$router.push({ name: 'role_mgr_list' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.el-card__header {
  padding: 4px 20px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  #background: #409eff;
  #color: #ffffff;
  font-size: 15px;
  border-bottom: none;
}
.el-divider--horizontal {
  display: block;
  height: 1px;
  width: 100%;
  margin-top: 10px;
  margin-bottom: 24px;
}
</style>
